import sublime
import sublime_plugin
from .SideBarAPI import SideBarItem, SideBarSelection, SideBarProject
from .apis.apis import scss_txt
import os
# from console import *
Pref = {}
s = {}
Cache = {
    "cached":False
}
def Window(window=None):
    return window if window else sublime.active_window()

def CACHED_SELECTION(paths=[]):
    # if Cache.cached:
    #     return Cache.cached
    # else:
    return SideBarSelection(paths)
class createFilesVueCommand(sublime_plugin.WindowCommand):
    def run(self, paths=[], name=""):
      # self.view.insert(edit, 0, "Hello, World!")
        print("输出测试:",name,paths)
        import functools
        Window().run_command("hide_panel")
        view = Window().show_input_panel(
            "输入文件名:",
            name,
            functools.partial(self.on_done, paths, False),
            None,
            None,
        )
        Window().focus_view(view)
    def on_done(self, paths, relative_to_project, name):
        _paths = paths
        _pathsfiles = name
        files = "index.vue"
        styleType = "scss"
        # sublime.error_message(""+str(paths)+str(name))
        # print("路径对象:",paths,"文件地址路径",name)
        # print("遍历path对象?",_paths)
        if "|" in name:
            _pathsfiles = name.split("|")[0]
            files = name.split("|")[1]
            styleType = name.split("|")[2]
        for item in _paths:
            print("新增文件夹?",item,"-",_pathsfiles,"-",)
            new_fold_path = os.path.join(item,_pathsfiles)
            item = SideBarItem(new_fold_path, True)
            if item.exists():
                sublime.error_message("无法创建文件夹、文件夹或文件,文件夹或文件已存在!")
                self.run(paths, name)
                return
            else:
                # print("item的对象:",item)
                item.create()
                if not item.exists():
                    sublime.error_message("无法创建文件夹:\n\n" + item.path())
                    self.run(paths, name)
                    return
            files_item = SideBarItem(os.path.join(new_fold_path,files),False)
            if not files_item.exists():
                files_item.create()
                scss_txt_cont = scss_txt.replace("$1",styleType)
                scss_txt_cont = scss_txt_cont.replace("$2",_pathsfiles)
                # format(styleType,_paths)
                print("写入的值的对象:",type(scss_txt_cont))
                files_item.write(scss_txt_cont)
                #  此处设置写入的值

        SideBarProject().refresh()

    # def is_enabled(self, paths=[]):
    #     return CACHED_SELECTION(paths).len() > 0